package com.ziytek.web.citizen.service;

import cn.idev.excel.FastExcel;
import com.ziytek.web.citizen.model.req.CapDetailQueryReq;
import com.ziytek.web.citizen.model.rsp.CapDetailQueryRsp;
import com.ziytek.web.citizen.model.vo.CapDetailVo;
import com.ziytek.web.citizen.model.vo.CapacityStatusDetailExportVo;
import com.ziytek.web.citizen.util.DateTimeUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 容量详情数据导出
 *
 * @author fenghx
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CapacityDetailExportService {
    private final CapacityDetailService capacityDetailService;

    public void capDetailExport(CapDetailQueryReq req, HttpServletResponse response) throws IOException {
        req.setPageNum(1);
        req.setPageSize(100000000);
        // 当天日期的
        CapDetailQueryRsp rsp;
        if (StringUtils.isNotBlank(req.getBatchNo()) && DateTimeUtils.isToday(req.getBatchNo())) {
            rsp = capacityDetailService.capDetailQuery(req);
        } else {
            rsp = capacityDetailService.historyCapDetailQuery(req);
        }

        List<CapacityStatusDetailExportVo> dataList = new ArrayList<>();
        if (rsp != null && !CollectionUtils.isEmpty(rsp.getData())) {
            dataList = getDetailDataList(rsp.getData());
        }

        // 设置响应头
        var name = "电流容量详情数据_" + req.getBatchNo()
                .replaceAll("-", "")
                .replaceAll(":", "")
                .replaceAll(" ", "")
                + ".xlsx";

        response.reset();
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(name, StandardCharsets.UTF_8));
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/zip; charset=utf-8");

        // 使用 FastExcel 导出
        FastExcel.write(response.getOutputStream())
                .sheet("电流容量详情数据")
                .head(CapacityStatusDetailExportVo.class)
                .doWrite(dataList);

    }

    private List<CapacityStatusDetailExportVo> getDetailDataList(List<CapDetailVo> vo) {
        if (CollectionUtils.isEmpty(vo)) {
            return Collections.emptyList();
        }

        List<CapacityStatusDetailExportVo> dataList = new ArrayList<>();
        int index = 1;
        for (var cv : vo) {
            if (cv == null) {
                continue;
            }

            var cve = new CapacityStatusDetailExportVo();
//            cve.setSeq(cv.getSeq());
            cve.setSeq(String.valueOf(index));
            cve.setCurrent(cv.getCurrent());
            cve.setVoltage(cv.getVoltage());
            cve.setState(cv.getState());
            cve.setTemperature(cv.getTemperature());
            cve.setChannelNo(cv.getChannelNo());
            cve.setBatteryNo(cv.getBatteryNo());
            cve.setCapacity(cv.getCapacity());
            cve.setTestTime(DateTimeUtils.toDate(cv.getExecutionTime(), null));
            dataList.add(cve);
            index++;
        }

        return dataList;
    }
}
